package scau.cn.write.os;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import scau.cn.component.abstracts.Disk;
import scau.cn.component.domain.Catalog;

public class WriteIntoDisk {
	private Catalog root;
	private static String path = "conf.txt";
	
	//---------------------------------方法----------------------------------------
	public void WriteIn(Catalog root1){
		this.root = root1;
		File file1 = new File(path);
		if(!file1.exists()) {
			try {
				file1.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file1));
			oos.writeObject(root);
			oos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Catalog ReadIn(){
		File file1 = new File(path);
		if(file1.exists()) {
			ObjectInputStream oos = null;
			try {
				oos = new ObjectInputStream(new FileInputStream(file1));
				root = (Catalog) oos.readObject();
				return root;
			} catch(Exception e) {
				e.printStackTrace();
			} finally {
				if(oos != null) {
					try {
						oos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		return null;
	}
}
